package cn.niit.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    /**
     * 配置 shiro 过滤器
     * @return
     */
         @Bean
            public ShiroFilterFactoryBean shiroFilterFactoryBean(){
                //** 创建 过滤器对象 *//*
                ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
                //** 配置 安全管理器 *//*
                shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager());

                //** 配置过滤器，主要告诉shiro 拦截哪些请求地址 *//*
                Map<String, String> filterMap = new LinkedHashMap<>();
                //放行注册页面
                filterMap.put("/user/register", "anon");
                //** 配置shiro拦截一切资源， authc 代表 shiro 中的一个 Filter 的别名 *//*
                filterMap.put("/**", "authc");
                shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
                //修改调整的登录页面
                shiroFilterFactoryBean.setLoginUrl("/login");
                //设置未授权提示页面
                //*shiroFilterFactoryBean.setUnauthorizedUrl("/noAuth");*//*

                return shiroFilterFactoryBean;
            }

    /**
     * 配置 安全管理器
     * @return
     */
    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm());
        return securityManager;
    }

    /**
     * @return
     */
    public HashedCredentialsMatcher hashedCredentialsMatcher(){
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        credentialsMatcher.setHashAlgorithmName("md5");
        credentialsMatcher.setHashIterations(1024);
        return credentialsMatcher;
    }

    /**
     * 自定义 Realm 配置
     * @return
     */
    @Bean
    public UserRealm userRealm(){
        UserRealm userRealm = new UserRealm();
        userRealm.setCredentialsMatcher(hashedCredentialsMatcher());

        /** 开启 Shiro 缓存管理器 */
        //        EhCacheManager cacheManager = new EhCacheManager();
        //        userRealm.setCacheManager(cacheManager);
       /* userRealm.setCachingEnabled(true);
        userRealm.setAuthenticationCachingEnabled(true);
        userRealm.setAuthenticationCacheName("authenticationCacheName");
        userRealm.setAuthorizationCachingEnabled(true);
        userRealm.setAuthorizationCacheName("authorizationCacheName");
        //RedisCacheManager
        userRealm.setCacheManager(new RedisCacheManager());*/

        return userRealm;
    }

    /**
     * 配置方言
     * @return
     */
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
